package com.sc.nft.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.dao.ActivityTaskPlanDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dao.ActivityTaskUserPriceDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.dto.ActivityTaskPlanAdminDTO;
import com.sc.nft.entity.ActivityTaskPlan;
import com.sc.nft.entity.ActivityTaskUserPrize;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.ActivityTaskPrizeVO;
import com.sc.nft.entity.vo.ActivityTaskRecordVO;
import com.sc.nft.service.ActivityTaskPlanService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (ActivityTaskPlan)表服务实现类
 *
 * @author 王伟成
 * @since 2023-09-15 16:05:20
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ActivityTaskPlanServiceImpl implements ActivityTaskPlanService {

    private final UserInfoDao userInfoDao;

    private final ActivityTaskPlanDao activityTaskPlanDao;
    private final ActivityTaskUserPriceDao activityTaskUserPriceDao;

    @Override
    public Page<ActivityTaskPlan> taskRecord(Page page, UserInfo userInfo, Long activityId) {
        return activityTaskPlanDao.taskRecord(page,userInfo.getId(),activityId);
    }

    @Override
    public Page<ActivityTaskPlanAdminDTO> adminTaskRecord(Page page, String phoneNo, Long activityId, String taskName) {
        Long userId = null;
        if(StrUtil.isNotBlank(phoneNo)){
            UserInfo userInfo = userInfoDao.getOneByUserTel(phoneNo);
            if(ObjectUtil.isNull(userInfo))
                return page;
            userId = userInfo.getId();
        }
        Page<ActivityTaskPlanAdminDTO> activityTaskPlanAdminDTOPage = activityTaskPlanDao.adminTaskRecord(page, userId, activityId, taskName);

        for (ActivityTaskPlanAdminDTO record : activityTaskPlanAdminDTOPage.getRecords()) {
            List<ActivityTaskUserPrize> taskPlans = activityTaskUserPriceDao.getByTaskPlanId(record.getId());
            List<ActivityTaskPrizeVO> prizeVOS = Lists.newArrayList();
            for (ActivityTaskUserPrize taskPrize : taskPlans) {
                ActivityTaskPrizeVO vo = new ActivityTaskPrizeVO();
                vo.setPrizeCount(taskPrize.getPrizeCount());
                vo.setPrizeName(taskPrize.getPrizeName());
                prizeVOS.add(vo);
            }
            record.setTaskPrizes(prizeVOS);
        }
        return activityTaskPlanAdminDTOPage;
    }
}
